package models

import (
	"errors"
	"gitee.com/lzqysoft/framework/common/tool"
	"gitee.com/lzqysoft/framework/common/uuid"
	"time"
)

type Organ struct {
	Id              string `xorm:"not null pk index unique VARCHAR(64)"`
	Onwer           string `xorm:"VARCHAR(64)"`
	Group           string `xorm:"VARCHAR(64)"`
	Creater         string `xorm:"VARCHAR(64)"`
	CreateAt        int64  `xorm:"BIGINT"`
	CreateOrg       string `xorm:"VARCHAR(64)"`
	Updater         string `xorm:"VARCHAR(64)"`
	UpdateAt        int64  `xorm:"BIGINT"`
	Deleter         string `xorm:"VARCHAR(64)"`
	DeleteAt        int64  `xorm:"BIGINT"`
	Isdelete        string `xorm:"VARCHAR(2)"`
	Status          string `xorm:"VARCHAR(2)"`
	Remark          string `xorm:"VARCHAR(255)"`
	Operation       int    `xorm:"INTEGER"`
	Obligate3       string `xorm:"VARCHAR(64)"`
	Obligate2       string `xorm:"VARCHAR(64)"`
	Obligate        string `xorm:"VARCHAR(64)"`
	Pid             string `xorm:"index VARCHAR(64)"`
	Name            string `xorm:"VARCHAR(255)"`
	Category        string `xorm:"VARCHAR(2)"`
	ProvinceCode    string `xorm:"VARCHAR(16)"`
	CityCode        string `xorm:"VARCHAR(16)"`
	CountyCode      string `xorm:"VARCHAR(16)"`
	Province        string `xorm:"VARCHAR(255)"`
	City            string `xorm:"VARCHAR(255)"`
	County          string `xorm:"VARCHAR(255)"`
	Address         string `xorm:"VARCHAR(512)"`
	JuridicalPerson string `xorm:"VARCHAR(64)"`
	Desc            string `xorm:"VARCHAR(512)"`
	Code            string `xorm:"VARCHAR(64)"`
	Contact         string `xorm:"VARCHAR(64)"`
	Phone           string `xorm:"VARCHAR(32)"`
	Domain          string `xorm:"VARCHAR(64)"`
	Context         string `xorm:"VARCHAR(64)"`
}

func (v *Organ) TableName() string {
	return "base_organ"
}
func (v *Organ) BeforeInsert() {
	v.Id = uuid.UUID()
	v.CreateAt = time.Now().Unix()
	v.UpdateAt = time.Now().Unix()
}

func (v Organ) Convert2Map() map[string]interface{} {
	return tool.Struct2Map(v)
}
func GetOrganById(id string) (*Organ, error) {
	organ := &Organ{Id: id}
	has, err := db.Get(organ)
	if !has {
		return nil, errors.New("no record")
	}
	if err != nil {
		return nil, err
	}
	return organ, err
}
func AddOrgan(organ *Organ) error {
	_, err := db.Insert(organ)
	return err
}
